<?php

namespace App\Http\Controllers;

use App\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;


class UserController extends Controller
{
    // 注册账号
    public function Login(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|unique:user|max:30',
            'pwd' => 'required',
        ]);

        $model = new Users();
        $data = $model->getAdd($request);

        if($data){
            return redirect('user');
        }else{
            return redirect('login-add');
        }
    }

    // 登录账号
    public function Logins(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'pwd' => 'required',
        ]);
        $model = new Users();
        $data = $model->getLogin($request);

        if($data){
            $model = new Users();
            $arr = $model->getEmail($data);
            return view('demo.email',['id'=>$data]);
        }else{
            return redirect('login-user');
        }
    }

    //绑定邮箱
    public function getEmail(Request $request)
    {
        $this->validate($request, [
            'email' => 'required',
        ]);
        $email = $request['email'];
        $model = new Users();
        $arr = $model->getEmails($request);
        if($arr){
            $msg = '绑定成功';
            $to = $email;
            $subject = '困';
            Mail::send(
                'demo.emails',['msg'=>$msg],
                function ($msg) use($to,$subject){
                    $msg->to($to)->subject($subject);
                });
            return redirect('login-show');
        }
       

    }

    //展示
    public function getShow()
    {
        $model = new Users();
        $data = $model->getShow();
        return view('demo.show',['arr'=>$data]);
    }

    //赠送积分展示
    public function getNumber(Request $request)
    {
        $id = $request['id'];
        $model = new Users();
        $data = $model->getShow();
        $datas = $model->getNumOne($id);
        $datas = json_decode($datas,true);
        $arr = $model->getOne($request);
        return view('demo.number',['arr'=>$data,'id'=>$id,'number'=>$datas['number']]);
    }

    // 修改积分
    public function getNum(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'number' => 'required',
        ]);

        $model = new Users();
        $data = $model->getUpdate($request);
        if($data){
            $msg = '你的好友赠送'.$request['number'].'积分';
            $to = '1755708600@qq.com';
            $subject = '困';
            Mail::send(
                'demo.emails',['msg'=>$msg],
                function ($msg) use($to,$subject){
                    $msg->to($to)->subject($subject);
                });
            return redirect('login-show');
        }else{
            return redirect('login-show');
        }
    }

}
